/* trash.c - Very, very, plan, basic skeleton of the shell. */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>		/* fork() */
#include <sys/types.h>		/* getpid() */
#include <sys/wait.h>		/* wait() */
#include <sys/types.h>		/* wait() */
#include "ShellLibrary.h"

int exec_builtin (char *args[]) {
    return 1;			/* 0: executed; 1: not executed */
}

int main (int argc, char **argv) {
    int pid;			/* The pid of the new process. */
    short id_cmd = -1;
    //int status;			/* The return status of a process. */
    char *command_line;
    char **args;

    command_line = malloc(CMDLINE_SIZE*sizeof(char));
    args = malloc(CMDLINE_MAX_ARGS*sizeof(char *));

    do {
        printf ("@: ");		/* Prompt */
        scanf ("%s", command_line); /* Read command line */

        args[0] = command_line;
        args[1] = NULL;

        ParseCommandLine(args);
	    if (exec_builtin(args) == 1) {	/* Builtin; else, try to fork-exec */

            VerifyCommand(args, &id_cmd);   /* The command is mapped by an id */

            if(id_cmd < 20) {
	   		    pid = fork ();		/* Cloning the process. */
	            if (pid < 0)			/* If pid < 0, fork failed. */
	        	    fatal();

	            if (pid > 0) {			/* Code executed by the father. */

                    ExecuteCommands(args, id_cmd);
		    	    //wait (&id_cmd);		/* Block until a child returns. */

		            /* printf ("%d\n", WEXITSTATUS(id_cmd)); */

                }
	            else {			/* Code executed by the child */

                    /*ExecuteCommands(args, id_cmd);
		    	    execv(args[0], args);
		            fprintf (stderr, "%s: command not found\n", args[0]);
                    id_cmd = -1;*/
	            }
		    }
            else {  /* The command must be executed by the father */


            }
        }
        else {


        }

	} while(id_cmd != -1);

    return EXIT_SUCCESS;
}
